package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.MsgTemplateMapper;
import com.eco.fanliapp.entity.MsgTemplate;
import com.eco.fanliapp.service.IMsgTemplateService;
import com.eco.fanliapp.utils.DateFormatUtils;
import com.eco.fanliapp.utils.EmojiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MsgTemplateServiceImpl extends ServiceImpl<MsgTemplateMapper, MsgTemplate> implements IMsgTemplateService {

    @Autowired
    private MsgTemplateMapper templateMapper;

    @Override
    public ServerResponse saveMsgTemplate(MsgTemplate msgTemplate) {
        if (StringUtils.isEmpty(msgTemplate)) {
            return ServerResponse.createByErrorMessage("保存异常");
        }
        Integer id = msgTemplate.getId();

        msgTemplate.setNewsHb(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsHb()));
        msgTemplate.setNewsHbTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsHbTitle()));
        msgTemplate.setNewsFs(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsFs()));
        msgTemplate.setNewsTaobao(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsTaobao()));
        msgTemplate.setNewsWeixin(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsWeixin()));
        msgTemplate.setNewsCashSucc(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsCashSucc()));
        msgTemplate.setNewsCashFail(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsCashFail()));
        msgTemplate.setNewsBigdelay(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsBigdelay()));
        msgTemplate.setNewsOrderPay(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderPay()));
        msgTemplate.setNewsOrderSucc(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSucc()));
        msgTemplate.setNewsOrderSettle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSettle()));
        msgTemplate.setNewsOrderFail(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderFail()));
        msgTemplate.setNewsOrderRights(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderRights()));
        msgTemplate.setNewsOrderSuperPay(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSuperPay()));
        msgTemplate.setNewsOrderSuperSucc(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSuperSucc()));
        msgTemplate.setNewsOrderSuperSettle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSuperSettle()));
        msgTemplate.setNewsOrderSuperFail(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSuperFail()));
        msgTemplate.setNewsOrderSuperRights(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSuperRights()));
        msgTemplate.setNewsOrderLevelPay(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderLevelPay()));
        msgTemplate.setNewsOrderLevelSucc(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderLevelSucc()));
        msgTemplate.setNewsOrderLevelSettle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderLevelSettle()));
        msgTemplate.setNewsOrderLevelFail(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderLevelFail()));
        msgTemplate.setNewsOrderLevelRights(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderLevelRights()));

        //任务红包
        msgTemplate.setNewsOrderTaskTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderTaskTitle()));
        msgTemplate.setNewsOrderTask(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderTask()));

        //标题
        msgTemplate.setNewsFsTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsFsTitle()));
        msgTemplate.setNewsCashSuccTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsCashSuccTitle()));
        msgTemplate.setNewsCashFailTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsCashFailTitle()));
        //订单
        msgTemplate.setNewsOrderPayTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderPayTitle()));
        msgTemplate.setNewsOrderSettleTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSettleTitle()));
        msgTemplate.setNewsOrderFailTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderFailTitle()));
        //一级粉丝订单
        msgTemplate.setNewsOrderSuperPayTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSuperPayTitle()));
        msgTemplate.setNewsOrderSuperSettleTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSuperSettleTitle()));
        msgTemplate.setNewsOrderSuperFailTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSuperFailTitle()));

        //二级粉丝订单
        msgTemplate.setNewsOrderLevelPayTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderLevelPayTitle()));
        msgTemplate.setNewsOrderLevelSettleTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderLevelSettleTitle()));
        msgTemplate.setNewsOrderLevelFailTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderLevelFailTitle()));


        msgTemplate.setNewsOrderSettleFreezeTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSettleFreezeTitle()));
        msgTemplate.setNewsOrderSettleFreeze(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderSettleFreeze()));
        msgTemplate.setNewsOrderRewardAccount(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderRewardAccount()));
        msgTemplate.setNewsOrderRewardAccountTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderRewardAccountTitle()));
        msgTemplate.setNewsOrderFreeFreeze(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderFreeFreeze()));
        msgTemplate.setNewsOrderFreeFreezeTitle(EmojiUtil.resolveToByteFromEmoji(msgTemplate.getNewsOrderFreeFreezeTitle()));
        if (id == 0 || id == null) {
            templateMapper.insert(msgTemplate);
            return ServerResponse.createBySuccess("保存成功");
        } else {
            msgTemplate.setUpdateTime(DateFormatUtils.timestamp());
            templateMapper.update(msgTemplate);
            return ServerResponse.createBySuccess("保存成功");
        }

    }

    @Override
    public ServerResponse findMsgTemplateAll() {
        List<MsgTemplate> templateList = templateMapper.findMsgTemplateAll();
        for (MsgTemplate msgTemplate : templateList) {
            msgTemplate.setNewsHbTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsHbTitle()));
            msgTemplate.setNewsHb(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsHb()));
            msgTemplate.setNewsFs(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsFs()));
            msgTemplate.setNewsTaobao(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsTaobao()));
            msgTemplate.setNewsWeixin(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsWeixin()));
            msgTemplate.setNewsCashSucc(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsCashSucc()));
            msgTemplate.setNewsCashFail(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsCashFail()));
            msgTemplate.setNewsBigdelay(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsBigdelay()));
            msgTemplate.setNewsOrderPay(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderPay()));
            msgTemplate.setNewsOrderSucc(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSucc()));
            msgTemplate.setNewsOrderSettle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSettle()));
            msgTemplate.setNewsOrderFail(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderFail()));
            msgTemplate.setNewsOrderRights(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderRights()));
            msgTemplate.setNewsOrderSuperPay(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSuperPay()));
            msgTemplate.setNewsOrderSuperSucc(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSuperSucc()));
            msgTemplate.setNewsOrderSuperSettle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSuperSettle()));
            msgTemplate.setNewsOrderSuperFail(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSuperFail()));
            msgTemplate.setNewsOrderSuperRights(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSuperRights()));
            msgTemplate.setNewsOrderLevelPay(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderLevelPay()));
            msgTemplate.setNewsOrderLevelSucc(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderLevelSucc()));
            msgTemplate.setNewsOrderLevelSettle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderLevelSettle()));
            msgTemplate.setNewsOrderLevelFail(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderLevelFail()));
            msgTemplate.setNewsOrderLevelRights(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderLevelRights()));

            //任务红包
            msgTemplate.setNewsOrderTaskTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderTaskTitle()));
            msgTemplate.setNewsOrderTask(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderTask()));

            //标题
            msgTemplate.setNewsFsTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsFsTitle()));
            msgTemplate.setNewsCashSuccTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsCashSuccTitle()));
            msgTemplate.setNewsCashFailTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsCashFailTitle()));
            //订单
            msgTemplate.setNewsOrderPayTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderPayTitle()));
            msgTemplate.setNewsOrderSettleTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSettleTitle()));
            msgTemplate.setNewsOrderFailTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderFailTitle()));
            //一级粉丝订单
            msgTemplate.setNewsOrderSuperPayTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSuperPayTitle()));
            msgTemplate.setNewsOrderSuperSettleTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSuperSettleTitle()));
            msgTemplate.setNewsOrderSuperFailTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSuperFailTitle()));

            //二级粉丝订单
            msgTemplate.setNewsOrderLevelPayTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderLevelPayTitle()));
            msgTemplate.setNewsOrderLevelSettleTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderLevelSettleTitle()));
            msgTemplate.setNewsOrderLevelFailTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderLevelFailTitle()));


            msgTemplate.setNewsOrderSettleFreezeTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSettleFreezeTitle()));
            msgTemplate.setNewsOrderSettleFreeze(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderSettleFreeze()));
            msgTemplate.setNewsOrderRewardAccount(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderRewardAccount()));
            msgTemplate.setNewsOrderRewardAccountTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderRewardAccountTitle()));
            msgTemplate.setNewsOrderFreeFreeze(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderFreeFreeze()));
            msgTemplate.setNewsOrderFreeFreezeTitle(EmojiUtil.resolveToEmojiFromByte(msgTemplate.getNewsOrderFreeFreezeTitle()));
        }

        return ServerResponse.createBySuccess(templateList);
    }

    @Override
    public MsgTemplate findMsgTemplateLIMIT() {
        // TODO Auto-generated method stub
        return templateMapper.findMsgTemplateLIMIT();
    }

}
